<script lang="ts" setup>
import { AddRod, DelRod, getAreaList, RodList } from '@/api/rod';
import type { addRodParams, areaList, RodParams, RodRow } from '@/types/rod';
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus';
import { onMounted, ref } from 'vue';

const Params=ref<RodParams>({
  page:1,
  pageSize:10,
  poleName:'',
  poleNumber: '',
  poleStatus:''
})
const List=ref<RodRow[]>([])
const total=ref(0)
const getList=async ()=>{
  const res = await RodList(Params.value)
  List.value=res.data.rows
  total.value=res.data.total
}
onMounted(()=>{
  getList()
  getArea()
})
// 选中的每一项放入DelList，是用来删除的列表
const DelList = ref<number[]>([])
const selectable=(val:RodRow[])=>{
  DelList.value=val.map(item=>item.id)
}
// 删除一体杆
const DeleteRod=async (ids:number[])=>{
  if(ids.length===0){return ElMessage.info('请选择要删除的数据')}
  ElMessageBox.confirm(
    '确定删除吗?',
    '温馨提示',
    {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then( async () => {
     const res = await DelRod(ids)
      if(res.code===10000){
        getList()
        ElMessage({
        type: 'success',
        message: '成功删除',
      })
      }else{
        ElMessage.error(res.msg)
      }
    })
    .catch(()=>{
      ElMessage.info('已取消删除')
    })
}
// 添加一体杆
const dialogVisible = ref(false)
const isShow=()=>{
  dialogVisible.value=true
}
const form=ref<addRodParams>({
  poleName:'',
  poleNumber:'',
  poleIp:null,
  areaId:null,
  poleType:''
})
const formRules=ref<FormRules<addRodParams>>({
  poleName: [
    { required: true, message: '请输入一体杆名称', trigger: 'blur' },
  ],
  poleNumber: [
    { required: true, message: '请输入一体杆编号', trigger: 'blur' },
  ],
  poleIp: [
    { required: true, message: '请输入一体杆IP', trigger: 'blur' },
  ],
  areaId: [
    { required: true, message: '请选择安装区域', trigger: 'change' },
  ],
  poleType: [
    { required: true, message: '请选择一体杆类型', trigger: 'change' },
  ]

})
// 获取关联区域下拉列表
const arealist=ref<areaList[]>([])
const getArea=async ()=>{
  const res = await getAreaList()
  arealist.value=res.data
}
const formRef=ref<FormInstance>()
const submit=async ()=>{
 await formRef.value?.validate()
 const res = await AddRod(form.value)
 if(res.code===10000){
  ElMessage.success('添加成功')
  getList()
  dialogVisible.value=false
}
}
</script>

<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">一体杆名称：</span>
      <el-input clearable placeholder="请输入内容" class="search-main" v-model="Params.poleName"/>
      <span class="search-label">一体杆编号：</span>
      <el-input clearable placeholder="请输入内容" class="search-main" v-model="Params.poleNumber" />
      <span class="search-label">运行状态：</span>
      <el-select style="width: 240px" v-model="Params.poleStatus">
        <el-option label="全部" value="" />
        <el-option label="正常" value="0" />
        <el-option label="异常" value="1" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="getList">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="isShow">添加一体杆</el-button>
      <el-button @click="DeleteRod(DelList)">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="List" @selection-change="selectable">
        <el-table-column type="selection"></el-table-column>
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column label="一体杆名称" prop="poleName" />
        <el-table-column label="一体杆编号" prop="poleNumber" />
        <el-table-column label="一体杆IP" prop="poleIp" />
        <el-table-column label="安装区域" prop="areaName" />
        <el-table-column label="一体杆类型" prop="poleType">
          <template #default="{row}">
           <el-tag v-if="row.poleType==='center'">中心杆</el-tag>
           <el-tag v-else-if="row.poleType==='entrance'">入口</el-tag>
           <el-tag v-else>出口</el-tag>
          </template>

        </el-table-column>
        <el-table-column label="运行状态" prop="poleStatus">
          <template #default="{row}">
            <el-tag v-if="row.poleStatus===1">异常</el-tag>
           <el-tag v-else-if="row.poleStatus===0">正常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{row}">
            <el-button size="small" type="text" >编辑</el-button>
            <el-button size="small" type="text" @click="DeleteRod([row.id])">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination
      v-model:current-page="Params.page"
      v-model:page-size="Params.pageSize"
      :page-sizes="[1, 2, 3, 4]"
      layout="total,sizes, prev, pager, next,jumper"
      :total="total"
      @change="getList"
       />
    </div>
    <div>
      <el-dialog
    v-model="dialogVisible"
    title="添加一体杆"
    width="580"
  >
    <div class="dialog-container">
      <el-form class="form" label-position="top" :model="form" :rules="formRules" ref="formRef">
        <el-form-item class="form-item" label="一体杆名称" prop="poleName">
          <el-input v-model="form.poleName" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="一体杆编号" prop="poleNumber">
          <el-input v-model="form.poleNumber" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="一体杆IP" prop="poleIp">
          <el-input v-model="form.poleIp" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="安装区域" prop="areaId">
          <el-select  v-model="form.areaId as number">
            <el-option v-for="(item,index) in arealist" :key="index" :label="item.areaName" :value="item.areaId" />
          </el-select>
        </el-form-item>
        <el-form-item label="一体杆类型" prop="poleType">
          <el-select  v-model="form.poleType">
            <el-option label="出口" value="export" />
            <el-option label="入口" value="entrance" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
.dialog-container {
  padding: 0px 80px;
}
.form-item{
  margin-bottom: 20px;
}
</style>